package com.sky.controller.admin;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 菜品的控制层
 *
 * @Author mr.zhang
 * Date 2025/5/20 22:02
 */
@RestController
@RequestMapping("/admin/dish")
@Slf4j
@Api("菜品管理")
public class DishController {
    @Autowired
    private DishService dishService;

    /**
     * 新增菜品
     * @param dto
     * @return
     */
    @PostMapping
    @ApiOperation("新增菜品")
    public Result add(@ApiParam(value = "菜品信息") @RequestBody DishDTO dto){

        dishService.add(dto);
        return Result.success();
    }

    /**
     * 分页条件查询菜品
     * @param dishPageQueryDTO
     * @return
     */
    @GetMapping("page")
    @ApiOperation("分页查询菜品数据")
    public Result<PageResult> listPage(DishPageQueryDTO dishPageQueryDTO){
        log.info("分页查询菜品数据:{}",dishPageQueryDTO);
        PageResult pageResult = dishService.listPage(dishPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 删除菜品
     * @param ids
     * @return
     */
    @DeleteMapping
    @ApiOperation("批量删除菜品")
    public Result delete(@RequestParam("ids") List<Integer> ids){
        log.info("删除菜品:{}",ids);
        try {
            dishService.delete(ids);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
        return Result.success();
    }

    /**
     * 根据id查询菜品
     */
    @GetMapping("{id}")
    @ApiOperation("根据id查询菜品")
    public Result<DishVO> getById(@PathVariable("id") Integer id){
        try {
            DishVO dishVO = dishService.getById(id);
            return Result.success(dishVO);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }


    /**
     * 修改菜品数据
     */
    @PutMapping
    @ApiOperation("修改菜品数据")
    public Result update(@RequestBody DishDTO dishDTO){
        log.info("修改菜品数据:{}",dishDTO);

        dishService.update(dishDTO);


        return Result.success();

    }

    /**
     * 修改菜品状态
     */
    @PostMapping("/status/{status}")
    @ApiOperation("修改菜品状态")
    public Result updateStatus(@RequestParam Long id , @PathVariable("status") Integer status){
        dishService.updateStatus(id,status);
        return Result.success();
    }


    /**
     * 根据分类查询菜品
     */
    @GetMapping("list")
    @ApiOperation("根据分类查询菜品")
    public Result<List<DishVO>> getByType(@RequestParam Long categoryId){
        List<DishVO> dishVOList = dishService.getByType(categoryId);
        log.info("根据分类查询菜品:{}",dishVOList);
        return Result.success(dishVOList);
    }
}
